package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.LayR;
import com.gxa.common.R;
import com.gxa.mapper.ContractMapper;
import com.gxa.mapper.EmplRoleMapper;
import com.gxa.param.ContractParam;
import com.gxa.param.TableParam;
import com.gxa.pojo.Contract;
import com.gxa.pojo.ContractCategory;
import com.gxa.pojo.Empl;
import com.gxa.pojo.EmplRole;
import com.gxa.service.ContractService;
import com.gxa.service.EmplService;
import com.gxa.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractMapper mapper;

    /**
     * 获取合同列表
     * @param param
     * @return
     */
    @Override
    public LayR list(ContractParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<Contract> contract = mapper.findAll(param);
        // 封装pageInfo
        PageInfo<Contract> pageInfo = new PageInfo<>(contract);
        List<Contract> list = pageInfo.getList();
        for (Contract contract1 : list) {
            System.out.println(contract1);
        }

        return new LayR(0, "success", pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public LayR add(Contract contract,String emplName) {
        Timestamp cur_time=new Timestamp(System.currentTimeMillis());
        //获取当前时间
        contract.setLastOpdate(cur_time);
        contract.setCreateTime(cur_time);
        contract.setEmplId(mapper.findEmplByName(emplName).getId());
        contract.setCreateName(emplName);
        int res=mapper.add(contract);
        if(res==1) {
            return new LayR(200, "创建合同成功！", contract);
        }
        else {
            return null;
        }
    }

    @Override
    public LayR edit(Contract contract,String emplName) {
        Timestamp cur_time=new Timestamp(System.currentTimeMillis());
        //获取当前时间
        contract.setLastOpdate(cur_time);
        contract.setEmplId(mapper.findEmplByName(emplName).getId());
        int res=mapper.update(contract);
        if(res==1) {
            return new LayR(200, "修改合同成功！", contract);
        }
        else {
            return null;
        }
    }

    @Override
    public LayR findEmplByName(String emplName) {
        Empl empl=mapper.findEmplByName(emplName);
        if (empl!=null ){
            return new LayR(200,"",empl);
        }

        return new LayR(0,"没有此处理人!",null);
    }

    @Override
    public LayR findAllCategory() {
        return   new LayR(200,"",mapper.findAllCategory());
    }

    @Override
    public LayR findAllStatus() {
        return   new LayR(200,"",mapper.findAllStatus());
    }

    @Override
    public R deleteAll(String[] ids) {
        mapper.deleteAll(ids);
        return new R(200, "删除成功！");
    }

    @Override
    public LayR submit(Integer id) {
        mapper.submit(id,3);
        return new LayR(200,"提交成功！");
    }

    @Override
    public LayR audit(Integer id,Integer statusId,String advice,String emplName) {

        mapper.updateContractAudit(id,statusId,mapper.findEmplByName(emplName).getId(),new Timestamp(System.currentTimeMillis()));
        mapper.addAudit(id,statusId,advice);
        if(statusId ==4){
            return new LayR(200,"合同通过成功！");
        }
        return new LayR(200,"合同打回成功！");
    }

    @Override
    public Contract findById(Integer id) {
        return mapper.findById(id);
    }
}
